namespace DotNetNuke.Security.Permissions.Controls
{
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Security.Permissions;
    using DotNetNuke.Security.Roles;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Web.UI.WebControls;

    public class ModulePermissionsGrid : PermissionsGrid
    {
        private bool _InheritViewPermissionsFromTab = false;
        private int _ModuleID = -1;
        private ModulePermissionCollection _ModulePermissions;
        private ArrayList _Permissions;
        private ArrayList _roles;
        private int _ViewColumnIndex;

        public override void GenerateDataGrid()
        {
            if (this.ModuleID == -1)
            {
                this.ModuleID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(this.ViewState["ModuleID"]));
            }
            this.SetPermissions();
            BoundColumn textCol = new BoundColumn();
            textCol.HeaderText = "";
            textCol.DataField = "Roles";
            this.Columns.Add(textCol);
            int refIntHelperL0 = this._Permissions.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                PermissionInfo objPermission = (PermissionInfo) this._Permissions[i];
                CheckBoxColumn checkCol = new CheckBoxColumn();
                string locName = DotNetNuke.Services.Localization.Localization.GetString(objPermission.PermissionName + ".Permission", "~/App_GlobalResources/GlobalResources.resx");
                checkCol.HeaderText = Interaction.IIf(locName != "", locName, objPermission.PermissionName).ToString();
                checkCol.DataField = objPermission.PermissionName;
                this.Columns.Add(checkCol);
            }
            this.DataSource = this.GetPermissionsDataTable();
            this.DataBind();
            this.DynamicColumnAdded = true;
        }

        private DataTable GetPermissionsDataTable()
        {
            int i;
            DataTable dt = new DataTable();
            DataColumn col = new DataColumn("Roles");
            dt.Columns.Add(col);
            int refIntHelperL0 = this._Permissions.Count - 1;
            for (i = 0; i <= refIntHelperL0; i++)
            {
                PermissionInfo objPerm = (PermissionInfo) this._Permissions[i];
                col = new DataColumn(objPerm.PermissionName);
                if (objPerm.PermissionKey == "VIEW")
                {
                    this._ViewColumnIndex = i + 1;
                }
                dt.Columns.Add(col);
            }
            DataRow row = dt.NewRow();
            int refIntHelperL1 = this._roles.Count - 1;
            for (i = 0; i <= refIntHelperL1; i++)
            {
                RoleInfo role = (RoleInfo) this._roles[i];
                row = dt.NewRow();
                row[0] = DotNetNuke.Services.Localization.Localization.LocalizeRole(role.RoleName);
                int refIntHelperL2 = this._Permissions.Count - 1;
                for (int j = 0; j <= refIntHelperL2; j++)
                {
                    string key;
                    PermissionInfo objPerm = (PermissionInfo) this._Permissions[j];
                    string CheckBoxEnabled = "Y";
                    ModulePermissionInfo objModulePermission = this.ModuleHasPermission(objPerm.PermissionID, this._ModulePermissions, role.RoleName);
                    if (objModulePermission != null)
                    {
                        if (role.RoleID == PortalController.GetCurrentPortalSettings().AdministratorRoleId)
                        {
                            CheckBoxEnabled = "N";
                        }
                        key = "|" + Convert.ToString(objPerm.PermissionID) + "|" + Convert.ToString(objModulePermission.ModulePermissionID) + "|" + role.RoleName + "|" + role.RoleID.ToString() + "|" + CheckBoxEnabled;
                        if (objModulePermission.AllowAccess | (CheckBoxEnabled == "N"))
                        {
                            row[j + 1] = "True" + key;
                        }
                        else
                        {
                            row[j + 1] = "False" + key;
                        }
                    }
                    else
                    {
                        if (role.RoleID == PortalController.GetCurrentPortalSettings().AdministratorRoleId)
                        {
                            CheckBoxEnabled = "N";
                        }
                        key = "|" + Convert.ToString(objPerm.PermissionID) + "||" + role.RoleName + "|" + role.RoleID.ToString() + "|" + CheckBoxEnabled;
                        row[j + 1] = "False" + key;
                    }
                }
                dt.Rows.Add(row);
            }
            return dt;
        }

        private ModulePermissionInfo ModuleHasPermission(int PermissionID, ModulePermissionCollection colModulePermissions, string RoleName)
        {
            int refIntHelperL0 = colModulePermissions.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                ModulePermissionInfo objModulePermission = colModulePermissions[i];
                if ((objModulePermission.RoleName == RoleName) & (PermissionID == objModulePermission.PermissionID))
                {
                    return objModulePermission;
                }
            }
            return null;
        }

        private void SetPermissions()
        {
            this._roles = new RoleController().GetPortalRoles(PortalController.GetCurrentPortalSettings().PortalId);
            RoleInfo r = new RoleInfo();
            r.RoleID = int.Parse("-3");
            r.RoleName = "Unauthenticated Users";
            this._roles.Add(r);
            r = new RoleInfo();
            r.RoleID = int.Parse("-1");
            r.RoleName = "All Users";
            this._roles.Add(r);
            this._roles.Reverse();
            this._roles.Sort(new RoleComparer());
            this._ModulePermissions = new ModulePermissionController().GetModulePermissionsCollectionByModuleID(this.ModuleID);
            this._Permissions = new PermissionController().GetPermissionsByModuleID(this.ModuleID);
        }

        public bool InheritViewPermissionsFromTab
        {
            get
            {
                if (this.ViewState["InheritViewPermissionsFromTab"] != null)
                {
                    return Convert.ToBoolean(RuntimeHelpers.GetObjectValue(this.ViewState["InheritViewPermissionsFromTab"]));
                }
                return this._InheritViewPermissionsFromTab;
            }
            set
            {
                this._InheritViewPermissionsFromTab = value;
                this.ViewState["InheritViewPermissionsFromTab"] = value;
                if (value)
                {
                    int refIntHelperL0 = this.Items.Count - 1;
                    for (int i = 0; i <= refIntHelperL0; i++)
                    {
                        CheckBox chk = (CheckBox) this.Items[i].Cells[this._ViewColumnIndex].Controls[0];
                        chk.Checked = false;
                        chk.Enabled = false;
                    }
                }
                else
                {
                    int refIntHelperL1 = this.Items.Count - 1;
                    for (int i = 0; i <= refIntHelperL1; i++)
                    {
                        CheckBox chk = (CheckBox) this.Items[i].Cells[this._ViewColumnIndex].Controls[0];
                        if (int.Parse(Strings.Split(chk.ID, "|", -1, CompareMethod.Binary)[4]) == PortalController.GetCurrentPortalSettings().AdministratorRoleId)
                        {
                            chk.Checked = true;
                        }
                        else
                        {
                            chk.Enabled = true;
                        }
                    }
                }
            }
        }

        public int ModuleID
        {
            get
            {
                if (this._ModuleID != -1)
                {
                    return this._ModuleID;
                }
                return Convert.ToInt32(RuntimeHelpers.GetObjectValue(this.ViewState["ModuleID"]));
            }
            set
            {
                this._ModuleID = value;
                this.ViewState["ModuleID"] = value;
            }
        }

        public ModulePermissionCollection Permissions
        {
            get
            {
                IEnumerator refObjectHelperL0=null;
                PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
                ModulePermissionCollection arrModulePermissions = new ModulePermissionCollection();
                RoleController objRoleController = new RoleController();
                ModulePermissionController objModulePermissionController = new ModulePermissionController();
                try
                {
                    refObjectHelperL0 = this.Items.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        DataGridItem dgi = (DataGridItem) refObjectHelperL0.Current;
                        int refIntHelperL0 = dgi.Cells.Count - 1;
                        for (int i = 1; i <= refIntHelperL0; i++)
                        {
                            if (dgi.Cells[i].Controls.Count > 0)
                            {
                                CheckBox cb = (CheckBox) dgi.Cells[i].Controls[0];
                                if (cb.Checked)
                                {
                                    ModulePermissionInfo objModulePermission;
                                    RoleInfo objRoleInfo;
                                    string[] Settings = Strings.Split(cb.ID, "|", -1, CompareMethod.Binary);
                                    if (Settings[2] == "")
                                    {
                                        objModulePermission = new ModulePermissionInfo();
                                        objModulePermission.ModulePermissionID = -1;
                                    }
                                    else
                                    {
                                        objModulePermission = objModulePermissionController.GetModulePermission(Convert.ToInt32(Settings[2]));
                                    }
                                    objModulePermission.PermissionID = Convert.ToInt32(Settings[1]);
                                    string refStringHelperL0 = Settings[3];
                                    if (refStringHelperL0 == "All Users")
                                    {
                                        objRoleInfo = new RoleInfo();
                                        objRoleInfo.RoleID = Convert.ToInt32("-1");
                                        objRoleInfo.RoleName = "All Users";
                                        objRoleInfo.PortalID = _portalSettings.PortalId;
                                    }
                                    else if (refStringHelperL0 == "Unauthenticated Users")
                                    {
                                        objRoleInfo = new RoleInfo();
                                        objRoleInfo.RoleID = Convert.ToInt32("-3");
                                        objRoleInfo.RoleName = "Unauthenticated Users";
                                        objRoleInfo.PortalID = _portalSettings.PortalId;
                                    }
                                    else
                                    {
                                        objRoleInfo = objRoleController.GetRoleByName(_portalSettings.PortalId, Settings[3]);
                                    }
                                    objModulePermission.RoleID = objRoleInfo.RoleID;
                                    objModulePermission.AllowAccess = true;
                                    objModulePermission.ModuleID = this.ModuleID;
                                    arrModulePermissions.Add(objModulePermission);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
                return arrModulePermissions;
            }
        }
    }
}

